package com.llt.controller;

import com.llt.annotation.UserLoginToken;
import com.llt.enum_util.ResponseBodyEnum;
import com.llt.pojo.UserInfo;
import com.llt.pojo.common.AjaxResponseBody;
import com.llt.service.UserInfoService;
import com.llt.utils.Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(value = "用户管理",tags = {"用户管理"})
@RestController
@CrossOrigin
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "添加管理",response = UserInfo.class)
    @PostMapping("/addUser")
    @UserLoginToken
    @ResponseBody
    public AjaxResponseBody addUser(@Validated(UserInfo.class) @RequestBody UserInfo userInfo){
        AjaxResponseBody ajaxResponseBody = new AjaxResponseBody();
        //查询用户是否已经存在
        UserInfo data = userInfoService.userQueryByName(userInfo.getUsername());
        int result;
        if (data==null){
            String md5_2 = Utils.getMd5_2(userInfo.getPassword());
            userInfo.setPassword(md5_2);
            result = userInfoService.addUser(userInfo);
        }else {
            return new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_USER_FIND);
        }
        ajaxResponseBody.setData(result);
        ajaxResponseBody.setBodyEnum(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK);
        return ajaxResponseBody;
    }
    @GetMapping("/getUserInfoAll")
    @ApiOperation("获取所有管理")
    @UserLoginToken
    public AjaxResponseBody getUserInfoAll(){
        List<UserInfo> userInfos = userInfoService.userSelectAll();
        return new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK,userInfos);
    }
    @GetMapping("/deleteAdmin")
    @UserLoginToken
    @ResponseBody
    @ApiOperation(value = "删除二级分类")
    public AjaxResponseBody deleteAdmin(@RequestParam("id") Integer id){
        Integer data1 = userInfoService.deleteAdmin(id);
        if (data1==0){
            return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_SYSTEM_ERROR) ;
        }
        return  new AjaxResponseBody(ResponseBodyEnum.RESPONSE_BODY_ENUM_OK) ;
    }

}
